package com.example.backend.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend.api.common.Result;
import com.example.backend.api.entity.Project;
import com.example.backend.api.mapper.ProjectMapper;
import com.example.backend.api.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-07
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    public Result getAll(Integer projectId, String projectName) {
        System.out.println(projectId);
        if (projectId == null && projectName == null) {
            return Result.success(projectMapper.selectList(null));
        } else if (projectId != null && projectName == null ) {
            return Result.success(projectMapper.selectList(new QueryWrapper<Project>().eq("project_num", projectId)));
        } else if (projectId == null) {
            return Result.success(projectMapper.selectList(new QueryWrapper<Project>().like("name", projectName)));
        } else {
            return Result.success(projectMapper.selectList(new QueryWrapper<Project>().eq("project_num", projectId).like("name", projectName)));
        }

    }


    public Result delete(Integer id) {
        UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("project_num", id);
        projectMapper.delete(updateWrapper);
        return Result.success(null);
    }

    public Result getNotFinish() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return Result.success(projectMapper.selectList(queryWrapper));
    }

    public Result updateProject(String id, String name, String code, String startTime, String endTime, String status) throws ParseException {
        UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("project_num", id);
        updateWrapper.set("name", name);
        updateWrapper.set("status", status);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(startTime);
        updateWrapper.set("created_time", date);
        Date date2 = sdf.parse(endTime);
        updateWrapper.set("end_time", date2);
        updateWrapper.set("created_time", startTime);
        updateWrapper.set("end_time", endTime);
        updateWrapper.set("code", code);
        int res = projectMapper.update(null, updateWrapper);
        return Result.success(res);
    }

    public Result addProject(String name, String code, String startTime, String endTime, String status, Integer projectNum) throws ParseException {

        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_num", projectNum);
        if (projectMapper.selectOne(queryWrapper) != null) {
            return Result.fail("项目已存在");
        }
        Project project = new Project();
        project.setName(name);
        project.setCode(code);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(startTime);
        project.setCreatedTime(date);
        Date date2 = sdf.parse(endTime);
        project.setEndTime(date2);
        project.setStatus(status);
        project.setProjectNum(projectNum);
        projectMapper.insert(project);
        return Result.success(null);
    }
}
